//
//  vipOrderInDetailViewController.m
//  OrderMeal
//
//  Created by qkong on 4/24/14.
//  Copyright (c) 2014 周浩. All rights reserved.
//

#import "vipOrderInDetailViewController.h"
#import "orderDetailTableViewCell.h"
#import "MBProgressHUD.h"
#import "AFNetworking.h"
#import "UIView+Toast.h"
#import "vipOrderListViewController.h"
@interface vipOrderInDetailViewController ()
@end

@implementation vipOrderInDetailViewController
@synthesize orderDetails = _orderDetails;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    self.userName.text = [userDefaults objectForKey:@"username"];
    self.phone.text = [userDefaults objectForKey:@"phone"];
    self.address.text = [userDefaults objectForKey:@"address"];
    self.sumPrice.text = [NSString stringWithFormat:@"%@", self.orderDetails[@"turnover"]];
    NSString *orderStatus = [[NSString alloc]init];
    NSNumber *statusNum = self.orderDetails[@"status"];
    [self.orderBtn.layer setCornerRadius:8.0];
    if ([statusNum isEqualToNumber:@1]) {
        [self.orderBtn setTitle:@"取消" forState:UIControlStateNormal];
        orderStatus = @"待确认";
        [self.orderBtn addTarget:self action:@selector(cancelOrder:) forControlEvents:UIControlEventTouchUpInside];
    }else if([statusNum isEqualToNumber:@2]) {
        [self.orderBtn removeFromSuperview];
        orderStatus = @"已配送";
    }else if([statusNum isEqualToNumber:@3]) {
        [self.orderBtn setTitle:@"删除" forState:UIControlStateNormal];
        orderStatus =  @"已完成";
        [self.orderBtn addTarget:self action:@selector(deleteOrder) forControlEvents:UIControlEventTouchUpInside    ];
    }else if([statusNum isEqualToNumber:@4]) {
        [self.orderBtn setTitle:@"删除" forState:UIControlStateNormal];
        orderStatus = @"失败";
        }else if([statusNum isEqualToNumber:@9]) {
        [self.orderBtn setTitle:@"删除" forState:UIControlStateNormal];
        orderStatus = @"取消";
    }
    
    NSString *createTime = self.orderDetails[@"createDate"];
    self.orderTime.text = [[createTime componentsSeparatedByString:@" "] objectAtIndex:1];
    self.orderDateAndStatus.text = [NSString stringWithFormat:@"%@ %@",[[createTime componentsSeparatedByString:@" "] objectAtIndex:0],orderStatus];
    
    //tableview底部设置为空白
    UIView *viewClear = [UIView new];
    [viewClear setBackgroundColor:[UIColor clearColor]];
    [self.tableView setTableFooterView:viewClear];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.orderDetails[@"orderdetail"] count];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    orderDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"orderDetail"];
    int row = [indexPath row];
    cell.goodsName.text = self.orderDetails[@"orderdetail"][row][@"goods"][@"name"];
    cell.goodsNum.text = [NSString stringWithFormat:@"%@份",self.orderDetails[@"orderdetail"][row][@"number"]];
    cell.goodsSumPrice.text = [NSString stringWithFormat:@"￥%@",self.orderDetails[@"orderdetail"][row][@"sumPrice"]];
    return cell;
}


//取消订单
-(void)cancelOrder{
    UIAlertView *cancelReason = [[UIAlertView alloc]initWithTitle:@"取消原因" message:@"" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:@"取消", nil];
    [cancelReason show];
}

-(void)deleteOrder{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    NSDictionary *params = @{@"orderid":[NSString stringWithFormat:@"%@",self.orderDetails[@"id"]]};
    NSLog(@"orderdetail id ---->%@",self.orderDetails[@"id"]);
    [manager GET:orderdel_url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject){
        NSNumber *status=responseObject[@"success"];
        if ([status isEqualToNumber:@1]) {
            [self.navigationController popToRootViewControllerAnimated:YES];
            [[NSNotificationCenter defaultCenter]postNotificationName:@"vipOrderInDetail Delete Changed" object:nil];
        }else{
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"消息" message:@"删除失败" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
            [alert show];
        }
    }failure:^(AFHTTPRequestOperation *operation , NSError *error){
        NSLog(@"发生网络错误");
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"消息" message:@"删除失败" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
        [alert show];
    }];
    
}

//取消操作
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        NSDictionary *params = @{@"orderid":[NSString stringWithFormat:@"%@",self.orderDetails[@"id"]],
                                 @"reason":[alertView textFieldAtIndex:0].text,
                                 @"status": self.orderDetails[@"status"]};
        [manager GET:ordercancel_url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            if ([responseObject[@"status"] isEqualToNumber:@1]) {
                [self.view makeToast:@"修改成功" duration:2.0 position:@"center"];
                [[NSNotificationCenter defaultCenter]postNotificationName:@"vipOrderInDetail Cancel Changed" object:nil];
            }else{
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"消息" message:@"取消失败" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
                [alert show];
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"消息" message:@"取消失败" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
            [alert show];
        }];
    }else{
        
    }
}
@end
